package com.ct.controller.shipping;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.shipping.model.po.SysParameterSavePO;
import com.ct.application.shipping.model.po.SysParameterSearchPO;
import com.ct.application.shipping.model.vo.SysParameterVO;
import com.ct.application.shipping.service.ISysParameterService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.*;
import com.ct.commons.aspect.limiter.AccessIpLimiter;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统参数配置
 * @author Auto Generator
 * @since 2023-08-16
 */
@RestController
@PermissionApi(title = "系统参数参数",path="/api/v1/sysparameter",code = "Q1000",sorted = 13)
public class SysParameterController extends BaseController {

    @Resource
    private ISysParameterService sysParameterService;

    /**
     * 查询系统参数列表
     *
     * @param sysParameterSearchPO
     * @return
     */
    @PostMapping("/sysparameter/list")
    @PermissionApi(title = "查询系统参数列表",path="/api/v1/sysparameter/list",code = "Q1001",sorted = 1)
    public Page<SysParameterVO> findSysParameters(@Validated @RequestBody SysParameterSearchPO sysParameterSearchPO) {
        return sysParameterService.findSysParameters(sysParameterSearchPO);
    }

    /**
     * 获取系统参数列表
     * @return
     */
    @PostMapping("/sysparameter/get/list")
    @LoginIgnore
    @HasPermissionApi
    @PermissionApi(title = "查询系统参数列表",path="/api/v1/sysparameter/list",code = "Q1007",sorted = 7)
    public List<SysParameterVO> findSysParameters() {
        return sysParameterService.findSysParametersList();
    }

    /**
     * 保存和更新
     *
     * @param sysParameterSavePO
     * @return
     */
    @PostMapping("/sysparameter/saveupdate")
    @ApiLog("系统参数保存和更新")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "系统参数保存和更新",path="/api/v1/sysparameter/saveupdate",code = "Q1003",sorted = 3)
    public Boolean saveUpdateSysParameter(@Validated @RequestBody SysParameterSavePO sysParameterSavePO) {
        return sysParameterService.saveUpdateSysParameter(sysParameterSavePO);
    }

    /**
     * 根据id查询系统参数
     *
     * @param id
     * @return
     */
    @PostMapping("/sysparameter/get/{id}")
    @PermissionApi(title = "根据id查询系统参数",path="/api/v1/sysparameter/get/{id}",code = "Q1004",sorted = 4)
    public SysParameterVO getSysParameterById(@PathVariable("id") Long id) {
        return sysParameterService.getSysParameterById(id);
    }


    /**
     * 根据id删除系统参数
     *
     * @param id
     * @return
     */
    @PostMapping("/sysparameter/del/{id}")
    @ApiLog("根据id删除系统参数")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "根据id删除系统参数",path="/api/v1/sysparameter/del/{id}",code = "Q1005",sorted = 5)
    public Boolean delSysParameterById(@PathVariable("id") Long id) {
        return sysParameterService.deleteSysParameterById(id);
    }

    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/sysparameter/status")
    @ApiLog("系统参数修改状态")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "系统参数修改状态",path="/api/v1/sysparameter/status",code = "Q1006",sorted = 6)
    public Boolean updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return sysParameterService.updateStatus(id, field, Integer.parseInt(value));
        }else{
            return sysParameterService
                    .updateStatusStr(id, field, String.valueOf(value));
        }
    }
}
